const app = getApp();

Page({
   data:{
       typesArray:[
           {
               id: 2,
               name: '铝模工'
           },
           {
               id: 4,
               name: '木工'
           },
           {
               id: 5,
               name: '钢筋工'
           },
           {
               id: 6,
               name: '混凝土工'
           },
           {
               id: 7,
               name: '架子工'
           },
           {
               id: 8,
               name: '水电工'
           },{
               id: 9,
               name: '泥瓦工'
           },
           {
               id: 10,
               name: '铝合金不锈钢工'
           },
           {
               id: 11,
               name: '幕墙工'
           },
           {
               id: 12,
               name: '杂工'
           },
           {
               id: 13,
               name: '杂工'
           },{
               id: 14,
               name: '园林绿化'
           },{
               id: 15,
               name: '道路桥梁'
           },{
               id: 16,
               name: '城市照明'
           },{
               id: 17,
               name: '通讯工程'
           },{
               id: 18,
               name: '给排水'
           },{
               id: 19,
               name: '水电'
           },{
               id: 20,
               name: '油漆'
           },{
               id: 21,
               name: '泥水'
           },{
               id: 22,
               name: '其他'
           },
       ],
       type:'',
       time: '',
       types:[],
       count: 1,
       other:'',
       phone:'',
       pname:'',
       name:'',
       salary:0,
       men:0,
       lng:'',
       lat:'',
       typeName:[
        
       ] 
   },
    onLoad(){
        app.axios('/xmgz','GET',response=>{
            if (response){
                let data = response[0]
                console.log(response)
                this.setData({
                    phone:data.phone,
                    pname:data.name,
                    name:data.x_name
                })
            }
        })
    },
    bindTime(e){
        this.setData({
            time:e.detail.value
        })
    },
    bindPickerTypeChange:function(e){
        let count = e.currentTarget.dataset.count;
        // console.log(this.data.typesArray[e.detail.value].name)
        let types = [...this.data.types]
      let typeName = [...this.data.typeName]
      // console.log(typeName);
        types[count] = this.data.typesArray[e.detail.value].id
      typeName[count] = this.data.typesArray[e.detail.value].name
      console.log(typeName[count])
        this.setData({
            types: types,
          typeName: typeName
        })
        // let types = [...this.data.types,e.detail.value];
        console.log(types)
        // this.setData({
        //     types: types
        // })
    },
    addItem(){
       this.setData({
           count:++this.data.count
       })
    },
    inputOther(e){
        let other = e.detail.value
        this.setData({
            other:other
        })
    },
    inputSalary(e){
       this.setData({
           salary:e.detail.value
       })
    },
    inputMen(e){
       this.setData({
           men:e.detail.value
       })
    },
    submit(){
       let {pname,phone,name, salary,time, men,types,other} = this.data;
       if (salary && time && men && types && other && pname && phone ){
           wx.getLocation({
               type: 'wgs84',
               success: (res)=> {
                   let latitude = res.latitude
                   let longitude = res.longitude
                   this.setData({
                       lat:latitude,
                       lng:longitude
                   })
               }
           })
           let {lat,lng} = this.data
           if (lat && lng){
               
               app.axios('/needwork','POST',response=>{
                   wx.navigateTo({
                       url:'/pages/pushSuccess/index'
                   })
               },{
                   name:pname,
                   phone:phone,
                   time:time,
                   addr:name,
                   sum:men,
                   work:types,
                   desc:other,
                   money:salary,
                   longitude:lng,
                   latitude:lat
               })
           } else {
               wx.showModal({
                   title: '错误',
                   content: '请允许获取位置',
                   showCancel: false
               })
           }

        }
    }
});
